<?php

$filePath = array(
	'1.php' => 'D:/htdocs/1.php',
	'2.php' => 'D:/htdocs/2.php',
	'3.php' => 'D:/htdocs/3.php',
	'a/a1.php' => 'D:/htdocs/a/a1.php',
	'b/a2.php' => 'D:/htdocs/a/a2.php'
);
zip('D:abc.zip', $filePath, 'D:/htdocs');

/**
 * 生成zip文件
 * @param $zipPath 压缩后的文件地址
 * @param $filePath 压缩前后的文件对应列表
 * @param $soureDir 需要压缩的文件夹
 */
function zip($zipPath, $filePath = array(), $soureDir = null) {
	if (strtolower(end(explode('.', $zipPath))) != 'zip') {
		die('打包格式必须为zip文件。');
	}
	if ($soureDir != null) {
		$files = array();
		getFiles($soureDir, $files);
		$filePath = $filePath + $files;
	}
	$zip = new ZipArchive();
	$res = $zip->open($zipPath, ZipArchive::CREATE);
	if ($res === true) {
		foreach ($filePath as $k => $v) {
			$zip->addFile($v, $k);
		}
		$zip->close();
	}
}

/**
 * 得到文件列表
 */
function getFiles($dir, &$files = array()) {
	if(!file_exists($dir) || !is_dir($dir)){return;}

	if(substr($dir, -1) == '/'){
		$dir = substr($dir, 0, strlen($dir) - 1);
	}

	$_files = scandir($dir);
	foreach($_files as $v) {
		//以.开头的文件忽略
		if(($v != '.' && $v != '..') && substr($v, 0, 1) != '.') {
			$path = $dir . '/' . $v;
			if(is_dir($path))
				getFiles($path, $files);
			else {
				$key = substr($path, strpos($path, '/') + 1);
				$files[$key] = $path;
			}
		}
	}
	return $files;
}